package com.controller;

import com.dao.IClasssDAOImpl;
import com.dao.IGradeDAOImpl;
import com.dao.IStudentDAOImpl;
import com.pojo.Classs;
import com.pojo.Grade;
import com.pojo.Student;
import com.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;

/**
 * @CreateTime: 2022-12-27  09:40
 * @Description: TODO
 * @Version: 1.0.0
 */
@WebServlet(value = "/student")
public class StudentController extends HttpServlet {

    // 班级
    IClasssDAOImpl classsDAO = new IClasssDAOImpl();
    // 年级
    IGradeDAOImpl gradeDAO = new IGradeDAOImpl();
    // 学生
    private IStudentDAOImpl studentDAO = new IStudentDAOImpl();

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String operation = req.getParameter("operation");

        // 1. 判断操作类型
        // 打开添加页面
        if ("openAdd".equals(operation)) {
            openAdd(req, res);
        }
        // 打开更新页面
        if ("openEdit".equals(operation)) {
            openUpdate(req, res);
        }

        // 增删改查
        if ("add".equals(operation)) {
            add(req, res);
        } else if ("delete".equals(operation)) {
            delete(req, res);
        } else if ("update".equals(operation)) {
            update(req, res);
        } else if ("query".equals(operation)) {
            query(req, res);
        }

    }

    private void openUpdate(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String id = req.getParameter("id");
        Student student = studentDAO.findById(Integer.parseInt(id));
        req.setAttribute("student", student);
        req.getRequestDispatcher("/edit.jsp").forward(req, res);
    }


    private void openAdd(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        List<Classs> classsList = classsDAO.findAllList();
        req.setAttribute("classsList", classsList);
        List<Grade> daoAllList = gradeDAO.findAllList();
        req.setAttribute("gradeList", daoAllList);
        req.getRequestDispatcher("/add.jsp").forward(req, res);
    }

    private void query(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        List<Student> allList = studentDAO.findAllList();
        req.setAttribute("allList", allList);
        req.getRequestDispatcher("list.jsp").forward(req, res);
    }

    //
    private void update(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String id = req.getParameter("id");
        String studentName = req.getParameter("studentName");
        String gender = req.getParameter("gender");
        String comprehensiveAchievements = req.getParameter("comprehensiveAchievements");
        String studentClass = req.getParameter("studentClass");
        String studentGrade = req.getParameter("studentGrade");
        Student student = new Student();
        student.setId(Integer.parseInt(id));
        student.setStudentName(studentName);
        student.setGender(gender);
        student.setComprehensiveAchievements(comprehensiveAchievements);
        student.setStudentClass(studentClass);
        student.setStudentGrade(studentGrade);
        studentDAO.update(student);
        req.getRequestDispatcher("student?operation=query").forward(req, res);
    }

    private void delete(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String id = req.getParameter("id");
        studentDAO.delete(Integer.parseInt(id));
        req.getRequestDispatcher("student?operation=query").forward(req, res);
    }

    private void add(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        String studentName = req.getParameter("studentName");
        String gender = req.getParameter("gender");
        String comprehensiveAchievements = req.getParameter("comprehensiveAchievements");
        String studentClass = req.getParameter("studentClass");
        String studentGrade = req.getParameter("studentGrade");
        Student student = new Student();
        student.setStudentName(studentName);
        student.setGender(gender);
        student.setComprehensiveAchievements(comprehensiveAchievements);
        student.setStudentClass(studentClass);
        student.setStudentGrade(studentGrade);
        studentDAO.add(student);
        req.getRequestDispatcher("student?operation=query").forward(req, res);
    }


}
